Dao = function() {}

Dao.prototype.loadThread = function(title) {
	return null;
}

Dao.prototype.saveThread = function(thread) {
}

Dao.prototype.savePage = function(page) {
}

Dao.prototype.loadPage = function(number) {
}

Dao.prototype.savePost = function(post) {
}

Dao.prototype.loadPost = function(post) {
}

Dao.prototype.loadSession = function(id) {
	var str = eval('localStorage.S_' + id);
	if (str != null && str.length > 0) {
		return JSON.parse(str);
	}
	return null;
}

Dao.prototype.loadSessions = function(callback) {
	if (this.sessions == null) {
		this.sessions = new Array();
		var str = localStorage.sessions;
		if (str != null && str.length > 0) {
			var array = JSON.parse(str);
			for (var i in array) {
				var s = array[i];
				var session = new Session(callback);
				session.clone(s);
				this.sessions.push(session);
			}
		}
	}
	return this.sessions;
}

Dao.prototype.deleteSession = function(session) {
	for (var i in this.sessions) {
		var existing = this.sessions[i];
		if (existing.id == session.id) {
			this.sessions.splice(i, 1);
			break;
		}
	}

	localStorage.sessions = JSON.stringify(this.sessions);
}

Dao.prototype.saveSession = function(session) {
	if (session.thread == null && session.pendings.length == 0) return; // no need to save it
	if (this.sessionIds == null) {
		this.loadSessions();
	}
	for (var i in this.sessions) {
		var existing = this.sessions[i];
		if (existing.id == session.id) {
			this.sessions.splice(i, 1);
			break;
		}
	}
	if (session.thread != null)
		session.thread.pages = new Array(); // save spaces
	this.sessions.push(session);
	this.sessions.sort(__sort_session);
	localStorage.sessions = JSON.stringify(this.sessions);
}

function __sort_session(s1, s2) {
	return s2.id - s1.id;
}

Dao.prototype.loadPostUrl = function() {
	return localStorage.post_url;
}

Dao.prototype.savePostUrl = function(url) {
	localStorage.post_url = url;
}

Dao.prototype.getUrlPrefix = function() {
	return localStorage.url_prefix;
}

Dao.prototype.saveUrlPrefix = function(prefix) {
	localStorage.url_prefix = prefix;
}

Dao.prototype.loadSignPages = function() {
	var str = localStorage.sign_pages;
	if (str == null || str == '') return new Array();
	return JSON.parse(str);
}

Dao.prototype.saveSignPages = function(ary) {
	localStorage.sign_pages = JSON.stringify(ary);
}

Dao.prototype.getSpaceUrl = function() {
	return localStorage.space_url;
}

Dao.prototype.saveSpaceUrl = function(url) {
	localStorage.space_url = url;
}

Dao.prototype.loadManagedModules = function() {
	var str = localStorage.managed_modules;
	if (str == null || str == '') return new Array();
	var ary = JSON.parse(str);
	var ret = new Array();
	for (var i in ary) {
		var a = ary[i];
		var m = new ManagedModule(a);
		ret.push(m);
	}
	return ret;
}

Dao.prototype.saveManagedModules = function(ary) {
	localStorage.managed_modules = JSON.stringify(ary);
}

Dao.prototype.getLastSign = function() {
	return localStorage.last_sign;
}

Dao.prototype.saveLastSign = function(last_sign) {
	localStorage.last_sign = last_sign;
}

var dao = new Dao();
